package cn.south.campus.business.service.impl;

import cn.south.campus.business.mapper.StudentMapper;
import cn.south.campus.business.model.bo.LoginContext;
import cn.south.campus.business.model.bo.LoginInfo;
import cn.south.campus.business.model.entity.Student;
import cn.south.campus.business.model.enums.StudentEnums;
import cn.south.campus.business.model.vo.StudentVo;
import cn.south.campus.business.service.StudentService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 学生认证表 服务实现类
 * </p>
 *
 * @author chow
 * @since 2024-11-13
 */
@Service
@AllArgsConstructor
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    private StudentMapper studentMapper;

    @Override
    public int getAuthStatus(Long userId) {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<Student>().eq(Student::getUserId, userId);
        Student student = studentMapper.selectOne(wrapper);
        if (student == null) {
            return StudentEnums.AuthStatusEnums.NO_AUTH.getCode();
        }
        return student.getStatus();
    }

    @Override
    public void auth(StudentVo req) {
        LoginInfo context = LoginContext.getContext();
        Long userId = context.getId();
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<Student>().eq(Student::getStudentNo, req.getStudentNo());
        Long count = studentMapper.selectCount(wrapper);
        AssertUtils.isLtE(count, 1L, CommonEnums.DATA_REPEAT_ERROR);
        wrapper = new LambdaQueryWrapper<Student>().eq(Student::getUserId, userId);
        Student student = studentMapper.selectOne(wrapper);
        if (student != null) {
            student.setName(req.getName());
            student.setStudentNo(req.getStudentNo());
            student.setCollege(req.getCollege());
            student.setMajor(req.getMajor());
            student.setClasses(req.getClasses());
            student.setPic(req.getPic());
            student.setStatus(StudentEnums.AuthStatusEnums.AUTHING.getCode());
            studentMapper.updateById(student);
        }else {
            student = new Student();
            student.setUserId(userId);
            student.setName(req.getName());
            student.setStudentNo(req.getStudentNo());
            student.setCollege(req.getCollege());
            student.setMajor(req.getMajor());
            student.setClasses(req.getClasses());
            student.setPic(req.getPic());
            student.setStatus(StudentEnums.AuthStatusEnums.AUTHING.getCode());
            studentMapper.insert(student);
        }
    }

    @Override
    public StudentVo getAuthInfo() {
        LoginInfo context = LoginContext.getContext();
        Long userId = context.getId();
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<Student>()
                .eq(Student::getUserId, userId);
        Student student = studentMapper.selectOne(wrapper);
        return StudentVo.convert(student);
    }
}
